﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace ModelLayer.DTO
{
    [DataContract]
    public class StationDTO
    {
        [DataMember]
        public int id { get; set; }

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public double longitude { get; set; }

        [DataMember]
        public double latitude { get; set; }

        [DataMember]
        public double full_battery_cost { get; set; }

        [DataMember]
        public virtual IEnumerable<BatteryDTO> battery_dtos { get; set; }



        public static StationDTO CreateDTO(Station station, BatteryType type)
        {
            if(station == null)
            {
                return null;
            }
            long epochStartDate = new DateTime(1970, 1, 1, 0, 0, 0).Ticks;
            StationDTO dto = new StationDTO() { id = station.id, latitude = station.latitude, longitude = station.longitude, name = station.name };
            if(station.Batteries != null)
            {
                dto.battery_dtos = station.Batteries.Where(b => b.battery_type_id == type.id).Select(b => new BatteryDTO {
                    id = b.id,
                    reservation_date_milis = b.BatteryReservations.Select(r => (r.time_of_reservation.Ticks  - epochStartDate) / 10000)
                });
            }
            if(station.Pricings != null && type != null)
            {
                dto.full_battery_cost = station.Pricings.Where(p => p.battery_type_id == type.id).Select(p => p.price).Single();
            }
            return dto;
        }
    }
}
